package com.cy.核心编程.stream.work;

import java.io.*;
import java.text.DecimalFormat;

public class HomeWork {
    public static void main(String[] args) throws Exception {
        DecimalFormat df = new DecimalFormat("0.00");
        //拿到第一行
        File f1 = new File("src/fruit.txt");
        BufferedReader br = new BufferedReader(new FileReader(f1));
        String s = br.readLine();
        s = s+"_总价";

        //创建一个新文件副本
        File f2 = new File("副本_fruit.txt");
        BufferedWriter bw = new BufferedWriter(new FileWriter(f2));
        //写入第一行
        bw.write(s);
        bw.newLine();

        //拿到后面的并且拆分
        String next = "";
        while((next = br.readLine())!=null){
            String[] s1 = next.split("_");
            double jg = Double.parseDouble(s1[1]);
            double dj = Double.parseDouble(s1[2]);
            double totle = jg * dj;
            String newtotle = df.format(totle);
            next = next + "_" + newtotle;
            //写入新文件
            bw.write(next);
            bw.newLine();
        }
        br.close();
        bw.flush();
        bw.close();
        //副本替换
        f1.delete();
        f2.renameTo(f1);

    }
}
